#include "optionsdialog.h"

OptionsDialog::OptionsDialog(QWidget *parent) :
    QDialog(parent)
{
    QVBoxLayout *main_layout = new QVBoxLayout;
    this->setLayout(main_layout);

    QHBoxLayout *image_editor_layout = new QHBoxLayout;
    QLabel *image_editor_label = new QLabel("Image Editor:");
    image_editor_layout->addWidget(image_editor_label);
    m_image_editor_line = new QLineEdit();
    image_editor_layout->addWidget(m_image_editor_line);
    QPushButton *image_editor_button = new QPushButton("Browse...");
    image_editor_layout->addWidget(image_editor_button);
    main_layout->addLayout(image_editor_layout);

    QHBoxLayout *script_editor_layout = new QHBoxLayout;
    QLabel *script_editor_label = new QLabel("Script Editor:");
    script_editor_layout->addWidget(script_editor_label);
    m_script_editor_line = new QLineEdit();
    script_editor_layout->addWidget(m_script_editor_line);
    QPushButton *script_editor_button = new QPushButton("Browse...");
    script_editor_layout->addWidget(script_editor_button);
    main_layout->addLayout(script_editor_layout);


    QHBoxLayout *button_layout = new QHBoxLayout;
    QPushButton *okButton = new QPushButton("OK");
    QPushButton *cancelButton = new QPushButton("Cancel");
    button_layout->addWidget(okButton);
    button_layout->addWidget(cancelButton);
    button_layout->setAlignment(Qt::AlignRight);
    main_layout->addLayout(button_layout);

    LoadOptions();

    connect(image_editor_button, SIGNAL(clicked()),
             this, SLOT(BrowseImageEditor()));
    connect(script_editor_button, SIGNAL(clicked()),
             this, SLOT(BrowseScriptEditor()));

    connect(okButton, SIGNAL(clicked()),
             this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()),
             this, SLOT(reject()));

}

void OptionsDialog::LoadOptions(){
    QSettings settings("JGCB","AlegriaED");
    m_image_editor_line->setText(settings.value("paths/image_editor","").toString());
    m_script_editor_line->setText(settings.value("paths/script_editor","").toString());
}

void OptionsDialog::SaveOptions(){
    QSettings settings("JGCB","AlegriaED");
    settings.setValue("paths/image_editor",m_image_editor_line->text());
    settings.setValue("paths/script_editor",m_script_editor_line->text());

}

void OptionsDialog::BrowseImageEditor(){
    QString fileName = QFileDialog::getOpenFileName(this,
         tr("Open"), "/home/", tr("(*.exe)"));
    if(!fileName.isNull())
        m_image_editor_line->setText(fileName);
}

void OptionsDialog::BrowseScriptEditor(){
    QString fileName = QFileDialog::getOpenFileName(this,
         tr("Open"), "/home/", tr("(*.exe)"));
    if(!fileName.isNull())
        m_script_editor_line->setText(fileName);
}
